package com.lh.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lh.entity.table.InvestmentProject;
import com.lh.entity.vo.InvestmentProjectBo;
import com.lh.entity.vo.InvestmentProjectVo;
import com.lh.mapper.InvestmentProjectMapper;
import com.lh.service.IInvestmentProjectService;
import com.lh.utils.Answer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


import java.util.*;
import java.util.stream.Collectors;

/**
 * 投资项目Service业务层处理
 *
 * @author Lion Li
 * @date 2025-08-15
 */
@RequiredArgsConstructor
@Service
public class InvestmentProjectServiceImpl implements IInvestmentProjectService {

    private final InvestmentProjectMapper baseMapper;

    @Override
    public Answer<Object> add(InvestmentProjectBo bo) {
        InvestmentProject entity = BeanUtil.copyProperties(bo, InvestmentProject.class);
        baseMapper.insert(entity);
        return Answer.ok("添加成功");
    }

    @Override
    public Answer<Object> edit(InvestmentProjectBo bo) {
        InvestmentProject entity = BeanUtil.copyProperties(bo, InvestmentProject.class);
        baseMapper.updateById(entity);
        return Answer.ok("修改成功");
    }

    @Override
    public Answer<Object> remove(Collection<Long> ids) {
        baseMapper.deleteBatchIds(ids);
        return Answer.ok("删除成功");
    }

    @Override
    public Answer<List<InvestmentProjectVo>> list(InvestmentProjectBo bo) {
        LambdaQueryWrapper<InvestmentProject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvestmentProject::getType, bo.getType());
        queryWrapper.orderByDesc(InvestmentProject::getDataTime);
        List<InvestmentProject> list = baseMapper.selectList(queryWrapper);
        List<InvestmentProjectVo> voList = list.stream()
                .map(item -> BeanUtil.copyProperties(item, InvestmentProjectVo.class))
                .collect(Collectors.toList());

        return Answer.ok(voList);
    }

    @Override
    public Answer<Object> queryById(Long id) {
        InvestmentProject investmentProject = baseMapper.selectById(id);
        return Answer.ok(investmentProject);
    }
}
